package sh.grapendaal.tsuushin.module.uno.engine.domain.achievement.impl;

import sh.grapendaal.tsuushin.module.uno.engine.domain.Card;
import sh.grapendaal.tsuushin.module.uno.engine.domain.Card.Suit;
import sh.grapendaal.tsuushin.module.uno.engine.domain.achievement.Achievement;
import sh.grapendaal.tsuushin.module.uno.engine.dto.GameAction;
import sh.grapendaal.tsuushin.module.uno.engine.dto.GameState;

public class ImDoingItForTheAchievementAchievement extends Achievement {

	@Override
	public String getName() {
		return "I'm Doing It For The Achievement";
	}

	@Override
	public String getDescription() {
		return "Being able to play without a wildcard but still drawing a card.";
	}

	@Override
	public boolean isEarned(GameState gameState) {
		if (gameState.getLastAction() != GameAction.CARD_DRAWN) {
			return false;
		}
		boolean ableToPlay = false;
		boolean drawnCardFound = false;
		for (Card card : gameState.getCurrentPlayer().getCards()) {
			if (gameState.getTopCard().validateCard(card)) {
				if (!drawnCardFound && gameState.getCurrentPlayer().getDrawnCard().equals(card)) {
					// Filter out the drawn card, but prevent filtering out doubles.
					drawnCardFound = true;
				} else if (card.getSuit() == Suit.WILDCARD) {
					return false;
				} else {
					ableToPlay = true;
				}
			}
		}
		return ableToPlay;
	}

}
